N = int(raw_input())
children = []
candies = []
for x in range(N):
    children.append(int(raw_input()))
    candies.append(1)

needAdjust = True
while needAdjust:
    needAdjust = False;
    for x in range(N-1):
        if children[x] > children[x+1] and candies[x] <= candies[x+1]:
            candies[x] = candies[x+1] + 1
            needAdjust = True
        elif children[x] < children[x+1] and candies[x] >= candies[x+1]:
            candies[x+1] =  candies[x] + 1
            needAdjust = True
    for x in range(N-2, -1, -1):
        if children[x] > children[x+1] and candies[x] <= candies[x+1]:
            candies[x] = candies[x+1] + 1
            needAdjust = True
        elif children[x] < children[x+1] and candies[x] >= candies[x+1]:
            candies[x+1] =  candies[x] + 1
            needAdjust = True
print(sum(candies))
